﻿using System;
using System.Globalization;
using Medianamik.Core;
using Medianamik.Core.Interfaces;

namespace Medianamik.Client.DAL
{
    public class NodeRepository : INodeRepository
    {
        public void Reorder(Guid id, Guid destinationId, DropPosition position)
        {
            NodeManager.ChangeNodeOrder(NodeManager.GetNode(id), destinationId, position);
        }

        public void SetActiveStatus(Guid id, CultureInfo culture, ActiveStatus status)
        {
            NodeManager.SetNodeActiveStatus(id, culture, status == ActiveStatus.Activated);
        }

        public INodeWrapper NewNode(IMedianamikDTO dto)
        {
            return NodeManager.NewNode<NodeWrapper>(dto.TypeId, CultureInfo.GetCultureInfo(dto.CultureName), dto.ParentNodeId);
        }

        public INodeWrapper GetExistingNode(IMedianamikDTO dto)
        {
            return NodeManager.GetNode<NodeWrapper>(dto.NodeId);
        }

        public void SaveNode(INodeWrapper node)
        {
            NodeManager.SaveNode(node);
        }

        public void PublishNode(INodeWrapper node, CultureInfo culture, DateTime startDate, DateTime? endDate)
        {
            NodeManager.PublishNode(node.InnerNode.ID, culture, startDate, endDate);
        }

        public void SaveAndPublishNode(INodeWrapper node, CultureInfo culture, DateTime startDate, DateTime? endDate)
        {
            NodeManager.SaveNode(node);
            NodeManager.PublishNode(node.InnerNode.ID, culture, startDate, endDate);
        }

        public void SaveNodeHandlingPositionAndParentNodeId(INodeWrapper node, Guid? parentNodeId, short? position)
        {
            if (parentNodeId.HasValue)
                node.InnerNode.ParentID = parentNodeId;
            if (position.HasValue)
                node.InnerNode.Position = position.Value;
            NodeManager.SaveNode(node);
        }

        public void SaveAndPublishNodeHandlingPositionAndParentNodeId(INodeWrapper node, Guid? parentNodeId, short? position, CultureInfo culture, DateTime startDate, DateTime? endDate)
        {
            if (parentNodeId.HasValue)
                node.InnerNode.ParentID = parentNodeId;
            if (position.HasValue)
                node.InnerNode.Position = position.Value;
            NodeManager.SaveNode(node);
            NodeManager.PublishNode(node.InnerNode.ID, culture, startDate, endDate);
        }
    }
}